/**
 * 全局管理
 */
class GlobalManager {
	public constructor() {
	}

	private static _instance:GlobalManager;
	public static instance():GlobalManager{
		if(!GlobalManager._instance){
			GlobalManager._instance = new GlobalManager();
		}
		return GlobalManager._instance;
	}

	// private _stage:egret.Stage;
	// public get stage(){
	// 	return this._stage;
	// }

	private uiLayer:BaseLayer;
	private sceneLayer:BaseLayer;
	private alertLayer:BaseLayer;
	private tipsLayer:BaseLayer;
	private guideLayer:BaseLayer;

	private _isInit:boolean = false;
	public init(stage:egret.Stage){
		if(!this._isInit){
			this._isInit = true;
			GameConst.stage = stage;
			egret.ImageLoader.crossOrigin = 'anonymous';
			RES.setMaxLoadingThread(8);
			this.uiLayer = new BaseLayer();
			this.sceneLayer = new BaseLayer();
			this.alertLayer = new BaseLayer();
			this.guideLayer = new BaseLayer();
			this.tipsLayer = new BaseLayer();
			stage.addChild(this.sceneLayer);
			stage.addChild(this.uiLayer);
			stage.addChild(this.alertLayer);
			stage.addChild(this.tipsLayer);
			stage.addChild(this.guideLayer);
			LogUtil.log('初始化游戏');
		}
	}

	/**
	 * 添加显示对象到指定的层级
	 */
	public addChildToLayer(child:egret.DisplayObject, layer:LayerType, index:number = -1){
		let lay:eui.UILayer = null;
		switch(layer){
			case LayerType.Scene:
				lay = this.sceneLayer;
				break;
			case LayerType.UI:
				lay = this.uiLayer;
				break;
			case LayerType.Alert:
				lay = this.alertLayer;
				break;
			case LayerType.Guide:
				lay = this.guideLayer;
				break;
			case LayerType.Tips:
				lay = this.tipsLayer;
				break;
		}
		if(lay && !lay.contains(child)){
			if(index > -1){
				lay.addChildAt(child, index);
			}else{
				lay.addChild(child);
			}
		}
	}

	public getLayer(Type)
	{
		let lay:eui.UILayer = null;
		switch(Type){
			case LayerType.Scene:
				lay = this.sceneLayer;
				break;
			case LayerType.UI:
				lay = this.uiLayer;
				break;
			case LayerType.Alert:
				lay = this.alertLayer;
				break;
			case LayerType.Guide:
				lay = this.guideLayer;
				break;
			case LayerType.Tips:
				lay = this.tipsLayer;
				break;
		}
		return lay;
	}

	/**
	 * 移除显示对象到指定的层级
	 */
	public removeChildToLayer(child:egret.DisplayObject, layer:LayerType){
		let lay:eui.UILayer = null;
		switch(layer){
			case LayerType.Scene:
				lay = this.sceneLayer;
				break;
			case LayerType.UI:
				lay = this.uiLayer;
				break;
			case LayerType.Alert:
				lay = this.alertLayer;
				break;
			case LayerType.Guide:
				lay = this.guideLayer;
				break;
			case LayerType.Tips:
				lay = this.tipsLayer;
				break;
		}
		if(lay.contains(child)){
			lay.removeChild(child);
		}
	}

	    //当前游戏宽度
    public curWidth():number{
        return egret.MainContext.instance.stage.stageWidth;
    }

    //当前游戏宽度
    public curHeight():number{
        return egret.MainContext.instance.stage.stageHeight;
    }

	//是不是iphonex
	public isIphonex():boolean{ 
		// if(egret.Capabilities.os == "iOS")
        // {
		// 	if((this.curHeight()/this.curWidth()) >= 2) {
		// 		return true;
        //     }
        // }
		// return false;
		return WeChatgGameManager.instance().judgeIsIphoneX() || this.isAllScreen();
	} 

	//是不是全面屏
	public isAllScreen():boolean{
		// if(egret.Capabilities.os == "Android")
        // {
		if((this.curHeight()/this.curWidth()) >= 2) {
			return true;
		}
        // }
		return false;
	}

	//是否是电脑
	public isWindows():boolean
	{
		return egret.Capabilities.os == "Windows PC";
	}
}